# 镜像名字
IMAGE_NAME=redis
# 镜像版本
IMAGE_VERSON="7"
# 平台
IMAGE_PLATFORM=""
if [[ $1 != "" ]];then
IMAGE_PLATFORM=.$1
fi
# 镜像
IMAGE="${IMAGE_NAME}:${IMAGE_VERSON}${IMAGE_PLATFORM}"
# 脚本所在目录
SHELL_DIR=$(cd $(dirname $0); pwd)
# 容器名字
CONTAINER_NAME="${IMAGE_NAME}_baimao"
# 容器文件夹
CONTAINER_DIR="${SHELL_DIR}/${CONTAINER_NAME}"
# 配置文件夹
CONFIG_DIR="${SHELL_DIR}/${CONTAINER_NAME}/${IMAGE_NAME}"

echo 参数验证
echo 镜像:${IMAGE}
echo 脚本执行目录:${SHELL_DIR}
echo 容器文件夹:${CONTAINER_DIR}
echo 配置目录:${CONFIG_DIR}

if [ -d "${CONFIG_DIR}" ];then
    echo "⭐️ ${CONFIG_DIR}文件夹已存在, 为保证安全本脚本不支持覆盖安装!"
    exit
fi 

if [[ "$(docker images -q ${IMAGE})" == "" ]];then
    echo "⭐️ 本地不存在 ${IMAGE} 开始拉取...."
    docker pull registry.cn-shanghai.aliyuncs.com/baimao/${IMAGE}
    docker tag registry.cn-shanghai.aliyuncs.com/baimao/${IMAGE} ${IMAGE}
    docker rmi registry.cn-shanghai.aliyuncs.com/baimao/${IMAGE}
fi

echo "⭐️ 创建容器文件夹"
mkdir ${CONTAINER_NAME}
cd ${CONTAINER_NAME}
echo "⭐️ 移除旧容器"
docker rm -f ${CONTAINER_NAME}
echo "⭐️ 构建docker-compose.yml"

cat << EOF > ${CONTAINER_DIR}/docker-compose.yml
version: '3'
services:
  ${IMAGE_NAME}:
    image: ${IMAGE}
    container_name: ${CONTAINER_NAME}
    restart: always
    ports:
      - 6379:6379
    command: redis-server --requirepass baimao1993
    networks:
      - bridge

networks:
  bridge:
    external:
      name: mysql_baimao_bridge
EOF
docker compose -f ${CONTAINER_DIR}/docker-compose.yml up -d
echo "⭐️ 启动成功"